﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using System.IO.Compression;
using System.Linq;

namespace SimJsonRpc
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebHostBuilder()
               .UseKestrel()
               .ConfigureServices(services =>
               {
                    services.AddRpcServer(new RpcHttpServerOptions { Path = "/rpc" });
                    services.AddResponseCompression(options =>
                    {
                        // options.EnableForHttps = false;
                        options.Providers.Add<GzipCompressionProvider>();
                        options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/json-rpc" });
                    });

                    services.Configure<GzipCompressionProviderOptions>(options =>
                    {
                        options.Level = CompressionLevel.Fastest;
                    });
               })
                .Configure(app =>
                {
                    app.UseResponseCompression();
                    app.UseSimRpcServer();
                })
               .Build();
            host.Run();
        }

        public interface IUserService : IService
        {
            [RpcMethod("com.test.user")]
            string GetUserName(int id);
        }

        public class UserService : IUserService
        {
            public string GetUserName(int id)
            {
                return "Hello world!";
            }
        }
    }
}
